<template>
  <div>
    <el-breadcrumb separator=">" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">添加月租用户</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column prop="id" label="编号" header-align="center" align="center" width="180"></el-table-column>
      <el-table-column prop="location" label="车位区域" header-align="center" align="center" width="180"></el-table-column>
      <el-table-column prop="locationNumber" header-align="center" align="center" label="车位编号"></el-table-column>
      <el-table-column label="操作" header-align="center" align="center">
        <template slot-scope="scope">
         <el-button style="background-color: #0096dc" @click="add(scope.row)">添加</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="添加月租用户" :visible.sync="dialogFormVisible">

        <div style="width: 500px;position: relative;left: 30px">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="名称" prop="realName">
              <el-input v-model="ruleForm.realName"></el-input>
            </el-form-item>
            <el-form-item label="身份证号码" prop="cardId">
              <el-input v-model="ruleForm.cardId"></el-input>
            </el-form-item>
            <el-form-item label="电话号码" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item style="margin: 0 130px;">
              <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
              <el-button @click="resetForm('ruleForm')" style="position: relative;right: 5px">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      ruleForm: {
        locationNumber:'',
        realName: '',
        cardId: '',
        phone: '',
      },

      rules: {
        realName: [
          {required: true, message: '姓名', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'}
        ],
        cardId: [
          {required: true, message: '身份证号码', trigger: 'blur'},
          {min: 18, max: 18, message: '输入正确的身份证号码', trigger: 'blur'}
        ],
        carId: [
          {required: true, message: '输入车牌号', trigger: 'blur'},
          {min: 7, max: 7, message: '输入七位的车牌号', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '收入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '输入正确的手机号', trigger: 'blur'}
        ],

      }
    };
  },
  methods: {
    add(row){
      this.dialogFormVisible=true;
      this.ruleForm.locationNumber=row.locationNumber;
    },
    loadParkingSpaceList() {
      let url = 'http://localhost:8080/ParkingSpace/getFreeSpaceList'
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.code == 200) {
          this.tableData = responseBody.data;
        }else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'success'
          });
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible=false;
          let url = 'http://localhost:8080/mouthUser/addMouthUser';
          console.log('url=' + url, this.ruleForm);
          // let formData = this.qs.stringify(this.ruleForm);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, this.ruleForm).then((response) => {
            let responseBody = response.data;
            console.log(this.ruleForm)
            if (responseBody.code == 200) {
              window.location.href = "http://localhost:8080/pay/openMonth"
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.loadParkingSpaceList();
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  },
  created() {
    this.loadParkingSpaceList()
  }
}
</script>
